package com.leyou.user.controller;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.threadlocals.UserHolder;
import com.leyou.common.utils.BeanHelper;
import com.leyou.user.dto.UserAddressDTO;
import com.leyou.user.entity.TbUserAddress;
import com.leyou.user.service.TbUserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户收货地址管理
 */
@RestController
public class AddressController {

    @Autowired
    private TbUserAddressService userAddressService;

    /**
     * 查询用户的所有收货信息
     *
     * @return
     */
    @GetMapping("/address/list")
    public ResponseEntity<List<UserAddressDTO>> findAddressList() {
        return ResponseEntity.ok(userAddressService.findAddressList());
    }
    /**
     * 根据 地址id 获取地址信息
     * @param id 地址id
     * @return 地址信息
     */
    @GetMapping("/address/byId")
    public ResponseEntity<UserAddressDTO> queryAddressById(@RequestParam("id") Long id){
        UserAddressDTO userAddressDTO = userAddressService.findById(id);
        return ResponseEntity.ok(userAddressDTO);
    }
    /**
     * 根据userid 和 id查询地址信息
     * @param addressId
     * @param userId
     * @return
     */
    @GetMapping("/address/byUser")
    public ResponseEntity<UserAddressDTO> queryAddressByUser(@RequestParam(name="id")Long addressId,
                                                             @RequestParam(name = "userid") Long userId){
        UserAddressDTO userAddressDTO = userAddressService.findByIdAndUserId(addressId,userId);
        return ResponseEntity.ok(userAddressDTO);
    }
    /**
     * 新增用户收货地址
     *
     * @param userAddressDTO
     * @return
     */
    @PostMapping("/address/save")
    public ResponseEntity<Void> saveAddress(@RequestBody UserAddressDTO userAddressDTO) {
        if (userAddressDTO == null) {
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }
        Long userId = UserHolder.getUserId();
        TbUserAddress tbUserAddress = BeanHelper.copyProperties(userAddressDTO, TbUserAddress.class);
//        设置用户id
        tbUserAddress.setUserId(userId);
//        不是默认地址
        tbUserAddress.setIsDefault(0);
        boolean b = userAddressService.save(tbUserAddress);
        if (!b) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
        return ResponseEntity.noContent().build();
    }

    /**
     * 修改用户收货地址
     *
     * @param userAddressDTO
     * @return
     */
    @PutMapping("/address/update")
    public ResponseEntity<Void> updateAddress(@RequestBody UserAddressDTO userAddressDTO) {
        if (userAddressDTO == null) {
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }
        TbUserAddress tbUserAddress = BeanHelper.copyProperties(userAddressDTO, TbUserAddress.class);
        boolean b = userAddressService.updateById(tbUserAddress);
        if (!b) {
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
        return ResponseEntity.noContent().build();
    }

    /**
     * 设置默认收货地址
     *
     * @param addressId
     * @return
     */
    @PutMapping("/address/setDefault/{id}")
    public ResponseEntity<Void> setDefaultAddress(@PathVariable(name = "id") Long addressId) {
        userAddressService.setDefaultAddress(addressId);
        return ResponseEntity.noContent().build();
    }

    /**
     * 删除 地址信息
     *
     * @param addressId
     * @return
     */
    @DeleteMapping("/address/{id}")
    public ResponseEntity<Void> delete(@PathVariable(name = "id") Long addressId) {
        userAddressService.delete(addressId);
        return ResponseEntity.noContent().build();
    }
}